package com.xique.park.bean.transform.web.ipc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.vo.web.ipc.ParkIpcVo;

/**
 * ParkIpcTransform
 *
 * @author xique
 */
public class ParkIpcTransform {

    /**
     * 数据库实体转化为前端vo
     */
    public static ParkIpcVo transformParkIpcVo(ParkDeviceNew parkDeviceNew) {
        ParkIpcVo parkIpcVo = new ParkIpcVo();
        parkIpcVo.setId(parkDeviceNew.getId());
        parkIpcVo.setName(parkDeviceNew.getName());
        parkIpcVo.setLaneId(parkDeviceNew.getLaneId());
        parkIpcVo.setDevCode(parkDeviceNew.getCode());
        parkIpcVo.setIp(parkDeviceNew.getIp());
        parkIpcVo.setModel(parkDeviceNew.getModel());
        parkIpcVo.setSort(parkDeviceNew.getSort());
        parkIpcVo.setIsMain(parkDeviceNew.getIsMain());
        parkIpcVo.setBoardType(parkDeviceNew.getControlType());
        parkIpcVo.setLaneType(parkDeviceNew.getLaneType());
        parkIpcVo.setStatus(parkDeviceNew.getStatus());
        String config = parkDeviceNew.getRemark();
        // 设备上传的配置
        if (StringUtils.isNotEmpty(config)) {
            JSONObject configObj = JSON.parseObject(config);
            if (null != configObj) {
                parkIpcVo.setMac(configObj.getString("mac"));
                parkIpcVo.setSoftVersion(configObj.getString("fw_ver"));
                parkIpcVo.setIpcVersion(configObj.getString("ipc_ver"));
            }
        }
        // 在线状态处理
        String onlineStatus = parkDeviceNew.getOnlineStatus();
        JSONObject onlineStatusObj;
        if (StringUtils.isNotEmpty(onlineStatus)) {
            onlineStatusObj = JSON.parseObject(onlineStatus);
        } else {
            onlineStatusObj = new JSONObject();
            onlineStatusObj.put("state", "offline");
            onlineStatusObj.put("onTime", "");
            onlineStatusObj.put("offTime", "");
        }
        parkIpcVo.setOnlineStatus(onlineStatusObj);
        parkIpcVo.setCreateTime(parkDeviceNew.getCreateTime());
        parkIpcVo.setUpdateTime(parkDeviceNew.getUpdateTime());
        return parkIpcVo;
    }
}
